package demo07;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;

/**
 * @author: WangPeng
 * @createTime: 2022/11/07 17:09
 * @description:
 * @version: v1.0
 */
public class Test02 {
    public static void main(String[] args) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = "2022-11-07 17:24:23";
        LocalDateTime localDateTime = LocalDateTime.parse(time, dtf);
        System.out.println("localDateTime = " + localDateTime);
    }

    /**
     * 把字符串转日期
     */
    public static void getParse(){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String time = "2022-11-07 17:24:23";
        LocalDateTime localDateTime = LocalDateTime.parse(time, dtf);
        System.out.println("localDateTime = " + localDateTime);
    }
    /**
     * 时间转字符串
     */
    public static void getFormat(){
        //获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);
        //自定义日期格式
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd  HH:mm:ss");
        //当前时间按自定义的格式进行转成字符串
        String format = now.format(dtf);
        System.out.println("format = " + format);
        String format1 = dtf.format(now);
        System.out.println("format1 = " + format1);
    }
}